package com.jorado.etl.service;

import com.jorado.basedata.BaseDataUtils;
import com.jorado.core.Result;
import com.jorado.core.utility.StringUtils;
import com.jorado.event.EventClient;
import com.jorado.fasttext.FastTexter;
import com.jorado.fasttext.FilterManager;
import com.jorado.fasttext.TrainerProxy;
import com.jorado.lexicon.model.NatureUnit;
import com.jorado.lexicon.postag.PosTagger;
import com.jorado.lexicon.postag.impl.HanlpRecognizer;
import com.jorado.search.core.service.impl.ExportToFileImpl;
import com.jorado.search.core.service.impl.FileExporter;
import org.springframework.context.annotation.Description;

import java.util.List;
import java.util.Map;

@Description("基于应聘文本数据的模型测试服务")
public final class ApplyFileFastTextTestService extends ExportToFileImpl<String> {

    static {
        PosTagger.getDefault().setRecognizer(new HanlpRecognizer());
    }

    private String trainFile = "d:/fasttext/train.txt";
    private String modelFile = "d:/fasttext/model";
    private int total;
    private int hit;
    private int not;

    private FilterManager filterManager;
    private FastTexter fastTexter;

    public ApplyFileFastTextTestService(String sourceFile, String distFile) {
        this(sourceFile, distFile, true);
    }

    public ApplyFileFastTextTestService(String sourceFile, String distFile, boolean deleteIfExists) {
        super(new FileExporter(sourceFile, true), distFile, deleteIfExists);
        this.filterManager = new FilterManager();
        TrainerProxy trainerProxy = new TrainerProxy(trainFile, modelFile);
        this.fastTexter = trainerProxy.train();
        this.setAfterFilter(this::after);
    }

    /**
     * 用户id,性别,职位类别ID,简历中当前城市id,简历中学校id,职位发布城市id,专业id,职位编号,投递日期
     * 0,     1,   006,      622,          2962,      801,        614,  CC000106451J90000384000,2017-10-11 17:52:50.670
     *
     * @param dataList
     */
    @Override
    protected void export(List<String> dataList) {

        total += dataList.size();

        for (String line : dataList) {

            String[] splits = line.split(",");

            String jobTypeId = splits[2];
            String jobType = BaseDataUtils.getSubJobTypeName(jobTypeId);
            if (jobType.equals("其他")) {

                not++;
                continue;
            }

            String schoolId = splits[4];
            String majorId = splits[6];

            if (majorId.equals("9999")) {

                not++;
                continue;
            }

            if (schoolId.equals("9999")) {
                schoolId = "";

            }


            StringBuilder sb = new StringBuilder();

            sb.append(schoolId).append(" ");
            sb.append(majorId).append(" ");

            String input = sb.toString();
            if (StringUtils.isNullOrWhiteSpace(input)) {
                not++;
                continue;
            }

            List<String> words = StringUtils.split(input, " ");
            List<Map.Entry<String, Float>> result = this.fastTexter.predict(3, words);
            for (Map.Entry<String, Float> entry : result) {

                if (entry.getKey().equals(jobTypeId)) {
                    hit++;
                    continue;
                }
            }
        }

    }

    protected Result after(Result result) {

        EventClient.getDefault().asyncSubmitLog(String.format("共抽取职位:[%d]个", total));

        double recall = hit / total;
        double precision = hit / (total - not);

        System.out.println("recall->" + recall);
        System.out.println("precision->" + precision);
        System.out.println("F-score:[(2*precision*recall)/(precision+recall)]->" + (2 * recall * precision) / (recall + precision));
        System.out.println("precision/recall->" + precision / recall);

        return Result.OK;
    }


    /**
     * 过滤
     *
     * @param input
     * @return
     */
    public String filter1(String input) {
        return filterManager.filter(input);
    }

    /**
     * 分词
     *
     * @param input
     * @return
     */
    public String segment(String input) {
        List<NatureUnit> natureUnits = PosTagger.getDefault().recognition(input);
        StringBuilder sb = new StringBuilder();
        for (NatureUnit natureUnit : natureUnits) {
            String word = natureUnit.getWord();
            //剔除单个字
            if (word.length() > 1) {
                sb.append(natureUnit.getWord());
                sb.append(" ");
            }
        }
        return sb.toString();
    }
}
